const {SportRecord} = require('@/models/sport');
const { clearObject } = require('@/utils');

const getSportRecords = async (req, res) => {
  try {
    const { user_id, user_name, sport_type, sport_name, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await SportRecord.getSportRecords(user_name, user_id, sport_type, sport_name, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const createSportRecords = async (req, res) => {
  try {
    const body = req.body;
    const params = clearObject(body);
    const sportRecord = await SportRecord.createSportRecords(params);
    res.status(200).send(sportRecord);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const updateSportRecords = async (req, res) => {
  try{
    const { id } = req.params;
    const body = req.body;
    const params = clearObject(body);
    const sportRecord = await SportRecord.updateSportRecords(id, params);
    res.status(200).send(sportRecord);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const deleteSportRecords = async (req, res) => {
  try {
    const { id } = req.params;
    const sportRecord = await SportRecord.deleteSportRecords(id);
    res.status(200).send(sportRecord);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
module.exports = {
  getSportRecords,
  createSportRecords,
  updateSportRecords,
  deleteSportRecords
}
